<?php
include('class/class_connect.php');
include('class/class_page.php');
include('class/class_news.php');
include('class/class_acts.php');
include('class/class_address.php');
include('class/class_workdays.php');
include('class/class_rating.php');
include('class/tabs/class_tabs.php');
include('class/abstract/class_mainObjectInfo.php');
include('class/city_rus.php');
include('class/class_prices.php');
include('class/class_checkGetData.php');


$aCheck=new checkGetData('f');
$aCheck->checkTableId($_GET['city'], $_GET['id']);
session_start();
$aConnect=new connect('f');
$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
if(strtolower($_SESSION['login'])!=strtolower($row['user']))header('location: registr.php');

if(!isset($_SESSION['login']))header('location: registr.php');

class aFirmPage extends page
{
	var $row=array();
	var $cityRus;
	function __construct()
	{
		$aConnect=new connect('f');
		$this->row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
		$this->cityRus=new city_rus_class();
		parent::__construct();
	}
	function title()
	{
		echo $this->row['name'].' '.$this->cityRus->city_rus_im($_GET['city']);
	}
	function maincontent()
	{
		echo ('
		<div class="photoFrame">
		</div>
		<div class="route">
		    <div class="routeEl">
				<a href="pcabinet.php?city='.$_GET['city'].'">Личный кабинет</a>
			</div>
			<div class="routeEl">
				<a href="my_firms.php?city='.$_GET['city'].'">Мои фирмы</a>
			</div>
			<div class="routeEl">
				'.$this->row['name'].'
			</div>
		</div>
		<script type="text/javascript" src="jquery/jquery.js">
		</script>
		<link rel="stylesheet" type="text/css" href="style/cabinet.css" />
		');
		$aFirmInfo=new aFirmInfo($this->row);
    	$aTab=new aFirmTabs($this->row);
	}
}

class aFirmTabs extends tabs
{
	var $row;
	var $text;
	var $price;
	function __construct($row=array())
	{
		$aConnect=new connect('p');
		$r=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE firms_id='".$_GET['id']."'"));
		($r['tab_name'])?$priceTabName=$r['tab_name']:$priceTabName='Прайс-лист';
		if($r)$this->price=$priceTabName;
		$labels=array('Основная', 'О компании', 'Новости', 'События', $priceTabName);
		$this->row=$row;
		parent::__construct($labels);
	}
	function content0()
	{
		$this->cab_panel($this->row['id']);
	}
	function content1()
	{
		$aConnect=new connect('f');
		$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
		$text=nl2br($row['stext']);
		echo ('
		<script type="text/javascript" src="scripts/showPhoto.js">
		</script>
		<link rel="stylesheet" type="text/css" href="style/aboutFirms.css" />

		<div class="firmsPhotos" align="center">
			<table class="photoTable" border="0">
				<tr>
			');
			if(file_exists('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].''))
			{
				for($i=1; $i<=5; $i++)
				{
					if(file_exists('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.jpg'))
					{
						echo ('
						<td class="aPhotoTr" align="center">
							<div class="aPhoto">
								<img class="firmsPhoto" id="firmsPhoto'.$i.'" src="pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.jpg">
							</div>
						</td>
						');
					}
					else if(file_exists('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.php'))
					{
						echo ('
						<td class="aPhotoTr" align="center">
							<div class="aPhoto">
								<img class="firmsPhoto" id="firmsPhoto'.$i.'" src="');
							include('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.php');
							echo ('
								">
							</div>
						</td>
						');
					}
				}
			}
			echo ('
				</tr>
			</table>
		</div>
		<div class="textTitle">
			'.$this->row['text_title'].'
		</div>
		<div class="firmText">
			'.$text.'
		</div>
		<div class="firmsPhotos" align="center">
			<table class="photoTable" border="0">
				<tr>
			');
			if(file_exists('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].''))
			{
				for($i=6; $i<=10; $i++)
				{
					if(file_exists('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.jpg'))
					{
						echo ('
						<td class="aPhotoTr" align="center">
							<div class="aPhoto">
								<img class="firmsPhoto" id="firmsPhoto'.$i.'" src="pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.jpg">
						    </div>
						</td>
						');
					}
					elseif(
					file_exists('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.php'))
					{
						echo ('
						<td class="aPhotoTr" align="center">
							<div class="aPhoto">
								<img class="firmsPhoto" id="firmsPhoto'.$i.'" src="

						');
						include('pictures/firmsphoto/'.$_GET['city'].'/'.$_GET['id'].'/'.$i.'.php');
						echo ('
							">
							</div>
						</td>
						');
					}
				}
			}
			echo ('
				</tr>
			</table>
		</div>
		<div class="cab-panel">
			<div class="cab-panel-link">
				<a href="addfirmsphoto.php?city='.$_GET['city'].'&id='.$_GET['id'].'" class="belink">Добавить фото</a>
			</div>
		</div>
		');

	}
	function content2()
	{
		$aNews=new news('firms', $_GET['city'], $_GET['id'], true);
		$newsConnect=new connect('fn');
		$newsesNum=mysql_num_rows(connect::select_all($_GET['city'], "WHERE firms_id='".$_GET['id']."'"));
		if($newsesNum==0)$newsesName="Новостей нет";
		else if($newsesNum<=3)
		{
			$newsesName="Все новости";
		}
		else $newsesName="Последние новости";
		echo ('<center><h4>'.$newsesName.'</h4></center>');
		$aNews->showNews('editfirmsnews.php?city='.$_GET['city'].'&id=', 4, 200, "950px");
		echo ('
		<div class="cab-panel">
		');
			echo ('
			<div class="cab-panel-link">
				<a class="belink" href="addfirmsnews.php?city='.$_GET['city'].'&id='.$_GET['id'].'">
					Добавить новость
				</a>
			</div>
			');
		echo ('
		</div>
		');

	}
	function content3()
	{
		$act=new acts();
		$act->showActs('firms', 4, 200, $_GET['id'], true, "950px");
		echo ('
		<div class="cab-panel">
   			<div class="cab-panel-link">
				<a class="belink" href="addfirmsacts.php?city='.$_GET['city'].'&id='.$_GET['id'].'">
					Добавить событие
				</a>
			</div>
		</div>
		');
	}
	function content4()
	{
		$aConnect=new connect('f');
    	$firmsRow=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
    	$aConnect=new connect('p');
    	$res=$aConnect->select_all($_GET['city'], "WHERE firms_id='".$_GET['id']."'");
    	if($priceExists=mysql_num_rows($res))
    	{
    		echo '
    		<link rel="stylesheet" type="text/css" href="style/price.css" />
    		';
    		$row=mysql_fetch_array($res);
    		echo ('
    		<div class="prices" align="center">
	    		<div class="showPrice" align="center">
	    		');
	    			$aShowPrice=new showPrice($row['stext']);
	    			$aShowPrice->showPrices($row['stext'], false, false, array(2,1));//, $firmsRow['net_id']);
	    		echo ('
		    		<div class="showPriceButton">
	    				<a class="belink" href="prices.php?city='.$_GET['city'].'&id='.$_GET['id'].'">Показать полностью</a>
	    			</div>
	    		</div>
	    		<div class="cab-panel">
	    			<div class="cab-panel-link">
						<a class="belink" href="editprices.php?city='.$_GET['city'].'&id='.$_GET['id'].'">
							Редактировать '.$row['tab_name'].'
						</a>
					</div>
				</div>
    		</div>
    		');
    	}
    	else
    	{
    		echo ('
    		<center><h3>Прайс-лист не заполнен</h3></center>
    		<div class="cab-panel">
	    		<div class="cab-panel-link">
					<a class="belink" href="addprices.php?city='.$_GET['city'].'&id='.$_GET['id'].'">
						Создать прайс-лист
					</a>
				</div>
			</div>

    		');
    	}
    	echo ('
    	');
	}
	function cab_panel($id)
	{
		if($this->price)
		{
			$link='editprices';
			$anchor='редактировать '.$this->price;
		}
		else
		{
			$link='addprices';
			$anchor='Создать прайс';
		}
		echo ('
		<div class="cab-panel">
			<div class="cab-panel-link">
				<a href="editfirms.php?city='.$_GET['city'].'&id='.$id.'" class="belink">Редактировать</a>
			</div>
			<div class="cab-panel-link">
				<a href="addfirmsphoto.php?city='.$_GET['city'].'&id='.$id.'" class="belink">Добавить Фото</a>
			</div>
			<div class="cab-panel-link">
				<a href="addfirmsnews.php?city='.$_GET['city'].'&id='.$id.'" class="belink">Добавить новость</a>
			</div>
			<div class="cab-panel-link">
				<a href="addfirmsacts.php?city='.$_GET['city'].'&id='.$id.'" class="belink">Добавить событие</a>
			</div>
			<div class="cab-panel-link">
				<a href="'.$link.'.php?city='.$_GET['city'].'&id='.$id.'" class="belink">'.$anchor.'</a>
			</div>
			');
			if(!$this->row['net_id'])
			{
				echo ('
				<br />
			<div class="cab-panel-link">
				<a href="addnets.php?city='.$_GET['city'].'&id='.$_GET['id'].'" class="belink">Создать сеть '.$this->row['name'].'</a>
			</div>
				');
			}
			echo ('
		</div>
		');
	}
}

class aFirmInfo extends mainObjectInfo
{
	var $row;
	function __construct($row=array())
	{
		$this->row=$row;
		parent::__construct();
	}
	function showPic()
	{
		echo ('
		<img src="pictures/firmspics/'.$_GET['city'].'/'.$this->row['id'].'.jpg?'.rand().'" title="'.$this->name.'">
		');
		$aFirmRating=new rating('firms');
		$aFirmRating->echo_rating(0, $this->row['rating'], $this->row['votes']);
		if($this->row['net_id'])
		{
			echo ('
			<div class="netLink">
				<a href="netscabinet.php?id='.$this->row['net_id'].'&city='.$_GET['city'].'">сеть '.$this->row['name'].'</a>
			</div>
			');
		}
	}
	function showAddress()
	{
		$address=new address();
		$address->showSeveralAddresses($this->row['address'], $this->row['link']);
	}
	function showshort()
	{
		echo $this->row['short'];
	}
	function showName()
	{
		echo $this->row['name'];
	}
}
class showPrice extends prices
{
	function showPrices($tables, $a, $b, $limit){
		parent::showTables($tables, $a, $b, $limit);
	}
	function showTd($tdClass, $tdId, $tdValue, $temp='')
	{
		if($tdClass=='img')
		{
			if(file_exists('pictures/pricepics/'.$_GET['city'].'/'.$_GET['id'].'/'.$tdId.'.jpg'))
				echo '<img src="pictures/pricepics/'.$_GET['city'].'/'.$_GET['id'].'/'.$tdId.'.jpg?'.rand().'" />';
		}
		elseif($tdClass=='name' || $tdClass=='brend' || $tdClass=='model')
			echo '<a href="products.php?city='.$_GET['city'].'&product='.$tdValue.'">'.$tdValue.'</a>';
		else
			echo $tdValue;
	}
	function showHeader($table, $header=array())
	{
		$headerEmty=true;
		foreach($header as $key=>$vol)
		{
			if($vol && $vol!='')$headerEmty=false;
		}
		if($header && !$headerEmpty)
		{
			echo ('
			<tr id="headerTr'.$table.'" class="headerTr">
			');
			for($i=1; $i<=count($header); $i++)
			{
				echo ('
				<td id="headerTd'.$table.$i.'" class="headerTd">
					'.$header[$i-1].'
				</td>
				');
			}
			echo ('
			</tr>
			');
		}
	}
	function showTabName($tabName, $table)
	{
		echo ('
		<h3>'.$tabName.'</h3>
		');
	}
	function showColType($table, $colType){}
}
$aPage=new aFirmPage();

?>